package form;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;
import dao.RoleDAO;
import dao.UserDAO;



/**
 * Servlet implementation class dangnhap
 */
@WebServlet("/dangnhap")
public class dangnhap extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String err,url;
	private int capdo;
	public dangnhap() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		toDo(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		toDo(request, response);
	}
	
	private void toDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf8");
		response.setCharacterEncoding("utf8");
		HttpSession session = request.getSession();

		String email = request.getParameter("email");
		String matkhau = request.getParameter("matkhau");
		
		if (email == "" && matkhau == "") {
			request.setAttribute("err", "Bạn chưa nhập email");
			request.getRequestDispatcher("dangnhap.jsp").forward(request,
					response);
		}
		try {
			if(UserDAO.login(email, matkhau) == true){
				User user = UserDAO.getUser(email);
				session.setAttribute("user", user);
				session.setAttribute("isLogin", "1");
				capdo = RoleDAO.getRole(email);
			}
			else{
				err ="Email không hợp lệ hoặc mật khẩu không đúng! Xin vui lòng nhập lại";	
				request.setAttribute("err", err);
				request.getRequestDispatcher("dangnhap.jsp").forward(request,
						response);
			}
			
			switch (capdo) {
			case 4:
				url ="/Admin_index.jsp";
				session.setAttribute("capdo", "4");
				break;
			case 3:
				url ="/Admin_index.jsp";
				session.setAttribute("capdo", "3");
				break;
			case 2:
				url ="/index.jsp";
				session.setAttribute("capdo", "2");
				break;
			case 1:
				url ="/index.jsp";
				session.setAttribute("capdo", "1");
				break;
			default:
				url ="/index.jsp";
				session.setAttribute("capdo", "1");
				break;
				
			}
			RequestDispatcher rd= getServletContext().getRequestDispatcher(url);
			rd.forward(request, response);
			
		} catch (ClassNotFoundException | SQLException e) {
			request.setAttribute("err", "Hệ thống đã bị lỗi! Chúng tôi sẽ nhanh chóng khắc phục");
			request.getRequestDispatcher("dangnhap.jsp").forward(request,
					response);
		}

	}

}
